<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="demo"></div>
    <button onclick="numbers.forEach(myFunction)">点我</button>
    <p id="demo_foreach">
        1.forEach()
        没有返回值。
        2.map()
        有返回值，可以return 出来。</p>
    <script>
        // array.map 
        var array = [3, 5, 7, 9];
        //将所有元素+1
        var result = array.map(function (item, index, array) {
            console.log(item, index, array); //logs [4,6,8,10]
            return item + 1;
        });
        console.log(result); //logs [4,6,8,10]
        console.log(array); //logs [3,5,7,9]

        // filter
        var ages = [32, 33, 16, 40];

        function checkAdult(age) {
            return age >= 18;
        }
        (function myFunction() {
            document.getElementById("demo").innerHTML = ages.filter(checkAdult);
        }())

        // foreach
        const array1 = ['a', 'b', 'c'];

        array1.forEach(element => console.log(element));
    </script>
</body>

</html>